//
//  ChangeTelephoneViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/23.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//**********修改手机号码**********
#import "ChangeTelephoneViewController.h"

@interface ChangeTelephoneViewController ()
@property(nonatomic,copy)UITextField *telephoneTextField;
@property(nonatomic,copy)UITextField *codeTextField;
@property(nonatomic,copy)UIButton *getCodeBtn;
@property(nonatomic,copy)UIButton *changeBtn;
@property(nonatomic,copy)UILabel *promptLab;
@property(nonatomic,copy)NSTimer *myTimer;
@property(nonatomic,assign)NSInteger myTime;
@property(nonatomic,copy)NSMutableString *telephoneStr;
@end

@implementation ChangeTelephoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    _myTime=30;
    [self createNavigation];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createUI
{
    //手机号码
    UITextField *telephoneTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, 20+44+20, self.view.frame.size.width-10*2, 40)];
    telephoneTextField.leftViewMode=UITextFieldViewModeAlways;
    UIImageView *telephoneLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 30, 30)];
    telephoneLeftView.image=[UIImage imageNamed:@"my_telephone"];
    telephoneTextField.leftView=telephoneLeftView;
    telephoneTextField.placeholder=@"请填写手机号码";
    telephoneTextField.font=[UIFont systemFontOfSize:15];
    telephoneTextField.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    telephoneTextField.backgroundColor=[UIColor whiteColor];
    telephoneTextField.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    telephoneTextField.layer.borderWidth=1.0;
    telephoneTextField.layer.cornerRadius=5.0;
    telephoneTextField.keyboardType=UIKeyboardTypeNumberPad;
    [self.view addSubview:telephoneTextField];
    
//    //划线
//    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(telephoneTextField.frame)+10, self.view.frame.size.width, 1)];
//    line.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
//    [self.view addSubview:line];
//    
//    //验证码
//    UITextField *codeTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(line.frame)+10, self.view.frame.size.width-10*2-90, 40)];
//    codeTextField.leftViewMode=UITextFieldViewModeAlways;
//    UIImageView *codeLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 30, 30)];
//    codeLeftView.image=[UIImage imageNamed:@"my_code"];
//    codeTextField.leftView=codeLeftView;
//    codeTextField.keyboardType=UIKeyboardTypeNumberPad;
//    codeTextField.placeholder=@"请填写验证码";
//    codeTextField.font=[UIFont systemFontOfSize:15];
//    codeTextField.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
//    codeTextField.backgroundColor=[UIColor whiteColor];
//    codeTextField.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
//    codeTextField.layer.borderWidth=1.0;
//    codeTextField.layer.cornerRadius=5.0;
//    [self.view addSubview:codeTextField];
    
//    //获取验证码
//    UIButton *getCodeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//    getCodeBtn.frame=CGRectMake(CGRectGetMaxX(codeTextField.frame)+10, codeTextField.frame.origin.y+6, 80, 24);
//    getCodeBtn.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
//    getCodeBtn.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
//    getCodeBtn.layer.borderWidth=1.0;
//    getCodeBtn.layer.cornerRadius=5.0;
//    getCodeBtn.titleLabel.font=[UIFont systemFontOfSize:12];
//    [getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
//    [getCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [getCodeBtn addTarget:self action:@selector(getCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:getCodeBtn];
//    
//    //提示
//    UILabel *promptLab=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(codeTextField.frame)+5, self.view.frame.size.width-10*2, 30)];
//    //promptLab.text=@"已经向135****3576发送了短信验证码(30秒后重发)";
//    promptLab.textColor=[UIColor grayColor];
//    promptLab.font=[UIFont systemFontOfSize:12];
//    promptLab.hidden=YES;
//    [self.view addSubview:promptLab];
    
    //确认修改
    UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    changeBtn.frame=CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMaxY(telephoneTextField.frame)+35, 150, 35);
    [changeBtn setTitle:@"确认" forState:UIControlStateNormal];
    [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changeBtn.titleLabel.font=[UIFont systemFontOfSize:20];
    changeBtn.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    changeBtn.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    changeBtn.layer.borderWidth=2.0;
    changeBtn.layer.cornerRadius=5.0;
    [changeBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBtn];
    
    _telephoneTextField=telephoneTextField;
   // _codeTextField=codeTextField;
    //_getCodeBtn=getCodeBtn;
    _changeBtn=changeBtn;
   // _promptLab=promptLab;
}

//获取验证码
-(void)getCodeBtnClick:(UIButton *)sender
{
    if ([self checkTel:_telephoneTextField.text]) {
        _getCodeBtn.userInteractionEnabled=NO;
        _promptLab.hidden=NO;
        NSString *telephone=_telephoneTextField.text;
        NSMutableString *str=[NSMutableString stringWithString:telephone];
        [str replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        _telephoneStr=str;
        //_promptLab.text=[NSString stringWithFormat:@"已经向%@发送了短信验证码(30秒后重发)",str];
        _getCodeBtn.backgroundColor=[UIColor grayColor];
        _getCodeBtn.layer.borderColor=[UIColor grayColor].CGColor;
        _myTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    }
}
//启动定时器
-(void)timerRun
{
    _myTime--;
    //NSLog(@"=========%ld",_myTime);
    if (_myTime==0) {
        [_myTimer invalidate];
        _myTimer=nil;
        _myTime=30;
        _getCodeBtn.userInteractionEnabled=YES;
        _promptLab.hidden=YES;
        _promptLab.text=nil;
        _getCodeBtn.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
        _getCodeBtn.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    }else{
        _promptLab.text=[NSString stringWithFormat:@"已经向%@发送了短信验证码(%ld秒后重发)",_telephoneStr,_myTime];
    }
    
}


//确认修改
-(void)changeBtnClick:(UIButton *)sender
{
    if (_telephoneTextField.text.length>0) {
        if ([self checkTel:_telephoneTextField.text]) {
            self.returnTelephone(_telephoneTextField.text);
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }else{
        [self.view showTiShiMessage:@"手机号码不能为空"];
    }
}

-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"修改手机号码";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 判断手机号码的正则表达式
- (BOOL)checkTel:(NSString *)str

{
    
    if ([str length] == 0) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号码不能为空" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [alert show];
        
        return NO;
        
    }
    
    //1[0-9]{10}
    
    //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
    
    //    NSString *regex = @"[0-9]{11}";
    
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号码格式不正确" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [alert show];
        return NO;
        
    }
    
    
    
    return YES;
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    if (_myTime!=30&&_myTime!=0) {
        [_myTimer invalidate];
        _myTimer=nil;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
